package cn.hycat.leetcode.easy;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 吕泽浩业
 * @version 1.0
 */
public class L67二进制求和 {
    public String addBinary(String a, String b) {
        int len1 = a.length() - 1;
        int len2 = b.length() - 1;
        boolean y = false;
        StringBuilder builder = new StringBuilder();
        while(len1 >= 0 && len2 >= 0) {
            if(a.charAt(len1) == '0' && b.charAt(len2) == '0') {
                builder.append(y == false ? '0' : '1');
                y = false;
                len1--;len2--;
            } else if(a.charAt(len1) == '1' && b.charAt(len2) == '1') {
                builder.append(y == false ? '0' : '1');
                y = true;
                len1--;len2--;
            } else {
                builder.append(y == false ? '1' : '0');
                y = y == false ? false : true;
                len1--;len2--;
            }
        }
        int len = len1 > len2 ? len1 : len2;
        String s = len1 > len2 ? a : b;
        StringBuilder builder1 = new StringBuilder();
        while(len >= 0) {
            if(s.charAt(len) == '0') {
                builder1.append(y == false ? '0' : '1');
                y = false;
            } else {
                builder1.append(y == false ? '1' : '0');
                y = y == false ? false : true;
            }
        }
        if(y) builder1.append('1');

        return new String(builder.append(builder1).reverse());
    }
}
